package com.xmkj.winmoney.mine.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.carbean.CanShuBean;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.GetsysparaMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.LogUtils;
import com.common.utils.SPUtils;
import com.common.widget.dialog.CommonDialog;
import com.xmkj.winmoney.BaseUrlURLActivity;
import com.xmkj.winmoney.MainActivity;
import com.xmkj.winmoney.R;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chen on 2018/4/2.
 */

public class SettingActivity extends BaseMvpActivity {
    @BindView(R.id.rl_aboat_us)
    RelativeLayout mRlAboatUs;
    @BindView(R.id.rl_clear)
    RelativeLayout mRlClear;
    @BindView(R.id.rl_device)
    RelativeLayout mRlDevice;
    @BindView(R.id.rl_kefu)
    RelativeLayout mRlKefu;
    @BindView(R.id.tv_logout)
    TextView mTvLogout;
    private Handler handler = new Handler();
    private CanShuBean infoBean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(mRlClear);
        attachClickListener(mRlDevice);
        attachClickListener(mTvLogout);
        attachClickListener(mRlAboatUs);
        attachClickListener(mRlKefu);
        getQuesTion();
    }

    private void getQuesTion() {
        CommonSubscriber<CanShuBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                infoBean = (CanShuBean) o;

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        GetsysparaMethods.getInstance().syspara(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID));
        rxManager.add(subscriber);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == mRlClear.getId()) {
            showProgressingDialog();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    dismissProgressDialog();
                    showToastMsg("清理完毕！");
                }
            }, 1500);
        } else if (view.getId() == mRlDevice.getId()) {
            gotoActivity(DeviceActivity.class);
        } else if (mTvLogout.getId() == view.getId()) {
            showgoBackDialog();
        } else if (mRlAboatUs.getId() == view.getId()) {
//            if (EmptyUtils.isNotEmpty(infoBean.abouturl)) {
                Intent intent1 = new Intent(context, BaseUrlURLActivity.class);
                intent1.putExtra(BaseUrlURLActivity.MAIN_URL, infoBean.abouturl);
                intent1.putExtra(BaseUrlURLActivity.TITILE, "关于我们");
                context.startActivity(intent1);
//            }

        }else if(mRlKefu.getId()==view.getId()){
            AndPermission.with(SettingActivity.this)
                    .requestCode(105)
                    .permission(Manifest.permission.CALL_PHONE)
                    .rationale(mRationaleListener)
                    .send();
        }
    }

    private void showgoBackDialog() {
        final CommonDialog dialog = newCommonDialog("你确实退出当前账号？");
        dialog.setSubmit(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*DataCenter.getInstance().deleteLoginDataInfo();
                EMClient.getInstance().logout(true);
                ActivityStack.getInstance().finishAllActivity();
                gotoActivity(LoginActivity.class);
                onBackPressed();*/
                SPUtils.setShareInt(Constans.UID, Integer.MAX_VALUE);
                SPUtils.setShareString(Constans.HASHID, "");
                SPUtils.setShareString(Constans.USERNAME, "");
                SPUtils.setShareString(Constans.MOBILE, "");
                SPUtils.setShareString(Constans.NAME, "");
                SPUtils.setShareString(Constans.FACADE, "");
                SPUtils.setShareString(Constans.EMAIL, "");
                SPUtils.setShareString(Constans.DETAILS_ID, "");
                SPUtils.setShareInt(Constans.FIRST, Integer.MAX_VALUE);
                onBackPressed();
                dialog.dismiss();
                gotoActivity(MainActivity.class);

            }
        });
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("系统设置");
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }

    //申请权限
    private RationaleListener mRationaleListener = new RationaleListener() {
        @Override
        public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
            new AlertDialog.Builder(SettingActivity.this)
                    .setTitle("友好提醒")
                    .setMessage("没有权限您不能拨打电话，请把权限赐给我吧!(如果设置不再提示,您可以在设置中打开权限)")
                    .setPositiveButton("好，给你", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            rationale.resume();// 用户同意继续申请。
                        }
                    })
                    .setNegativeButton("我拒绝", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            rationale.cancel(); // 用户拒绝申请。
                        }
                    }).show();
        }
    };

    @PermissionYes(105)
    private void openPic() {
        LogUtils.e("成功回调了-->");
        gotoCall();


    }

    private void gotoCall() {
        if (EmptyUtils.isEmpty(infoBean.mobile)) {
            return;
        }
        showTwoDialog("提示", infoBean.mobile, "拨打", "取消");
        setOnDialogViewClick(new OnDialogViewClick() {
            @Override
            public void leftViewClick() {
//                        showToastMsg("hha");
                Intent intent = new Intent(); // 意图对象：动作 + 数据
                intent.setAction(Intent.ACTION_CALL); // 设置动作
                Uri data = Uri.parse("tel:" + infoBean.mobile); // 设置数据
                intent.setData(data);
                startActivity(intent); // 激活Activity组件
            }

            @Override
            public void singleViewClick() {

            }
        });


    }

    @PermissionNo(105)
    private void injectPic() {
        LogUtils.e("失败回调了-->");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        // 只需要调用这一句，剩下的 AndPermission 自动完成。
        AndPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}
